<template>
  <el-dialog
    :title="!dataForm.noId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="公告标题" prop="noticeTitle" label-width="100px">
        <el-input v-model="dataForm.noticeTitle" placeholder="公告标题"></el-input>
      </el-form-item>

      <el-form-item label="公告内容"  label-width="100px">
        <vue-wangeditor
          ref="wangeditor"
          id="editor"
          :uploadImgUrl="editorUrl"
          @load="loadImg"
          @change="getInfo"
          v-model="dataForm.noticeContent"
          width="100%">
        </vue-wangeditor>
        <el-upload
          class="upload-demo"
          :action="editorUrl"
          :on-success="handleEditerSuccess"
          >
          <!-- <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传不超过30M视频文件</div> -->
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'
  import vueWangeditor from 'vue-wangeditor'
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          noId: '',
          noticeTitle: '',
          noticeContent: ''
        },
        dataRule: {
          noticeTitle: [
            { required: true, message: '公告标题不能为空', trigger: 'blur' }
          ],
          noticeContent: [
            { required: true, message: '公告内容不能为空', trigger: 'blur' }
          ]
        },
        editorUrl: ''
      }
    },
    components: {
      vueWangeditor
    },
    methods: {
      init (id) {
        this.visible = true
        this.submitVisible = false
        this.dataForm.noticeTitle = ''
        this.dataForm.noticeContent = ''
        this.dataForm.noId = id || ''
        this.$nextTick(() => {
          this.$refs.wangeditor.clear()
        })
        this.editorUrl = this.$http.adornUrl(`/pig-file/center/file/upload`)
        if (this.dataForm.noId) {
          this.$http({
            url: this.$http.adornUrl(`/pig-center-biz/mall/shopNotice/info`),
            method: 'post',
            params: this.$http.adornParams({
              'id': this.dataForm.noId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data)
              this.dataForm.noticeTitle = data.shopNotice.noticeTitle
              this.$refs.wangeditor.setHtml(data.shopNotice.noticeContent)
            }
          })
        }
      },
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/mall/shopNotice/${!this.dataForm.noId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'noId': this.dataForm.noId || undefined,
                'noticeTitle': this.dataForm.noticeTitle,
                'noticeContent': this.$refs.wangeditor.getHtml()
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      loadImg (originalName, resultText) {
        let a = JSON.parse(resultText)
        if (a && a.code === 0) {
          var url = this.openImg(a.url)
          this.$refs.wangeditor.insertImg(url)
        }
      },
      openImg (url) {
        if (url) {
          url = this.$http.adornUrl(`/pig-file/center/file/openImg?url=${url}`)
          return url
        } else {
          return null
        }
      },
      getInfo () {
        this.dataForm.noticeContent = this.$refs.wangeditor.getHtml()
      },
      handleEditerSuccess (res, file, fileList) {
        if (res && res.code === 0) {
          let url = res.url
          let param = `?url=${url}`
          axios.post(this.$http.adornUrl(`/pig-file/center/file/player` + param),
          {url: url}, {responseType: 'blob'})
            .then(response => {
              let urls = window.URL.createObjectURL(response.data)
              this.$refs.wangeditor.appendHtml(`<p><iframe src='` + urls + `' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true'></iframe></p>`)
            })
        } else {
          this.$message.error(res.msg)
        }
      }
    }
  }
</script>

<style>
</style>
